package com.lanswon.qzsmk.controller.systemManageController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.base.PathContants;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.exception.ErrorCode;
import com.lanswon.qzsmk.model.Personal;
import com.lanswon.qzsmk.service.cardManageService.ApplyCardService;
import com.lanswon.qzsmk.util.CommonUtil;
import com.lanswon.qzsmk.util.HttpUtil;
import com.lanswon.qzsmk.vo.ResultStatus;
import com.lanswon.qzsmk.vo.ResultVo;

@Controller
@RequestMapping(value="/personalInfo")
public class UpdatPersonalInfoController {
	@Autowired
	ApplyCardService applyCardService;

	@RequiresPermissions("personalInfo:index")
	@RequestMapping(value="index",name="修改个人信息首页,personalInfo:index",method={RequestMethod.POST,RequestMethod.GET})
	public String personalInfoIndex(){
		return "UpdatPersonal";
	}
	
	@RequiresPermissions("personalInfo:updatePersonalRegister")
	@RequestMapping(value="updatePersonalRegister",name="修改个人信息登记页,personalInfo:updatePersonalRegister",method={RequestMethod.POST})
	public String updatePersonalRegister(){
		return "UpdatePersonalRegister";
	}
	
	@RequiresPermissions("personalInfo:queryPersonalInfo")
	@ResponseBody
	@RequestMapping(value="/queryPersonalInfo",name="查询修改人信息,personalInfo:queryPersonalInfo",method=RequestMethod.POST)
	public List<Personal>  queryPersonalInfo(String paperNo,String paperType,String mobile) throws ApplicationException{
		List<Personal> personals=applyCardService.queryPersonal(paperType, paperNo, mobile);
		return personals;
	}
	
	@ResponseBody
	@RequiresPermissions("personalInfo:goUpdatePersonalRegister")
	@RequestMapping(value="/goUpdatePersonalRegister",name="去修改个人信息登记页,personalInfo:goUpdatePersonalRegister",method={RequestMethod.POST})
	public Map<String, Object> goUpdatePersonalRegister(String [] personalInfo,HttpServletRequest request) throws ApplicationException{
		Map<String, Object> map=new HashMap<>();
		String []persinfo=personalInfo[0].split("#");
		List<Personal> personal=applyCardService.queryPersonal(persinfo[0], persinfo[1], persinfo[2]);
		if(personal.size()==1){
			request.getSession().setAttribute("personal", personal.get(0));
		}else if(personal.size()>1){
			throw new ApplicationException(ErrorCode.OTHER_EXCEPTION, "查出2人信息无法修改");
		}
		map.put("statusCode", "200");
		map.put("callbackType", "forward");
		map.put("forwardUrl", "personalInfo/updatePersonalRegister");//post请求
		return map;
	}
	
	@ResponseBody
	@RequiresPermissions("personalInfo:getPersonalInfo")
	@RequestMapping(value="/getPersonalInfo",name="登记页面获取修改人信息,personalInfo:getPersonalInfo",method={RequestMethod.POST})
	public Personal getPersonalInfo(HttpServletRequest request){
		Personal personal=(Personal) request.getSession().getAttribute("personal");
		request.getSession().removeAttribute("personal");
		return personal;
		
	}
	
	@ResponseBody
	@RequiresPermissions("personalInfo:sendMessage")
	@RequestMapping(value="/sendMessage",name="发送手机验证码,personalInfo:sendMessage",method={RequestMethod.POST})
	public JSONObject sendMessage(String mobile,HttpServletRequest request) throws ApplicationException{
		List<NameValuePair> parameForToken = new ArrayList<NameValuePair>();
		Map<String, Object>map=new HashMap<>();
		String code=CommonUtil.romNum();
		map.put("code", code);
		JSONObject js=new JSONObject(map);
        parameForToken.add (new BasicNameValuePair("phoneNum", mobile));
        parameForToken.add (new BasicNameValuePair("templateParams", js.toJSONString()));
        String url=PathContants.MESSAGE_SERVICE_IP+PathContants.MESSAGE_SERVICE_PATH;
        JSONObject jsonObject=HttpUtil.doGetJSON(url, parameForToken);
        request.getSession().setAttribute("code", code);
		return jsonObject;
	}
	
	@ResponseBody
	@RequiresPermissions("personalInfo:commitPersonalInfo")
	@RequestMapping(value="/commitPersonalInfo",name="提交个人修改信息,personalInfo:commitPersonalInfo",method={RequestMethod.POST})
	public Map<String, String> commitPersonalInfo(Personal personal,String code,HttpServletRequest request) throws ApplicationException{
		Map<String, String>map=new HashMap<>();
		
		boolean b = applyCardService.checkExistPerson(personal.getPaperType(), personal.getPaperNo(),personal.getCustomerNo());
		
		if(b){
			map.put("statusCode", "300");
			map.put("message", "证件号码已存在");
			return map;
		}
		
		String code2=(String) request.getSession().getAttribute("code");
		if(!code.equals(code2)){
			throw new ApplicationException(ErrorCode.OTHER_EXCEPTION, "验证码不正确");
		}else{
			request.getSession().removeAttribute("code");
			applyCardService.modifyPersonal(personal);
		}
		map.put("statusCode", "200");
		map.put("message", "修改成功");
		map.put("navTabId", "personalInfo");
//		map.put("callbackType", "closeCurrent");
		map.put("forwardUrl", "personalInfo/index");//post请求
		return map;
	}
	
}
